package www.reagnetbox.com.util;

import android.app.Activity;
import android.content.Context;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;


import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import www.reagnetbox.com.R;
import www.reagnetbox.com.callback.SimpleCallback;

public class RecyclerViewUtil {

    public static void initRecyclerView(Context context, BaseQuickAdapter adapter) {
        RecyclerView rv = ((Activity) context).findViewById(R.id.rv);
        if (rv == null) {
            return;
        }
        rv.setHasFixedSize(true);
        rv.setLayoutManager(new LinearLayoutManager(context));
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        adapter.isFirstOnly(false);
        rv.setAdapter(adapter);
    }

    public  static SmartRefreshLayout initRefreshAndLoadMore(Context context, SimpleCallback onRefresh,SimpleCallback onLoadMore){
        SmartRefreshLayout refreshLayout =  ((Activity) context).findViewById(R.id.refreshLayout);
        refreshLayout.setEnableAutoLoadMore(false);

        if (onRefresh!=null){
            refreshLayout.setOnRefreshListener(refresh -> onRefresh.fun());
        }else{
            refreshLayout.setEnableRefresh(false);
        }

        if (onLoadMore!=null){
            refreshLayout.setOnLoadMoreListener(refresh -> onLoadMore.fun());
        }else{
            refreshLayout.setEnableLoadMore(false);
        }

        return refreshLayout;
    }
}
